package com.jcxy.service.impl;

import com.jcxy.mapper.DictionaryMapper;
import com.jcxy.pojo.Dictionary;
import com.jcxy.pojo.Status;
import com.jcxy.service.DictionaryService;
import com.jcxy.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Override
    public PageBean queryListByPage(Map dto, Integer page, Integer limit) {

        Integer count = dictionaryMapper.queryDictionaryCount(dto);
        PageBean pageBean = new PageBean(page, limit);
        dto.put("pageIndex",pageBean.getPageIndex());
        dto.put("limit",pageBean.getLimit());
        List<Dictionary> userList = dictionaryMapper.queryDictionaryByPage(dto);

        pageBean.setResponseData(count,userList);
        return pageBean;
    }

    @Override
    @Transactional
    public int updateDictionary(Map dto) throws Exception {

        int count = dictionaryMapper.updateDictionary(dto);
        if(count > 1){
            throw new Exception("更新数据字典异常！");
        }
        return count;
    }

    @Override
    @Transactional
    public void insertDictionary(Map dto) throws Exception {

        dictionaryMapper.insertDictionary(dto);

    }

    @Override
    @Transactional
    public int deleteDictionaryById(Integer dictionary_id) throws Exception {

        int count = dictionaryMapper.deleteDictionaryById(dictionary_id);
        if(count > 1){
            throw new Exception("删除数据字典异常！");
        }
        return count;
    }

    @Override
    public PageBean getStatusListByPage(Map dto, Integer page, Integer limit) {

        Integer count = dictionaryMapper.queryStatusCount(dto);
        PageBean pageBean = new PageBean(page, limit);
        dto.put("pageIndex",pageBean.getPageIndex());
        dto.put("limit",pageBean.getLimit());
        List<Status> statusList = dictionaryMapper.queryStatusByPage(dto);

        pageBean.setResponseData(count,statusList);
        return pageBean;
    }

    @Override
    @Transactional
    public int updateStatus(Map dto) throws Exception {

        int count = dictionaryMapper.updateStatus(dto);
        if(count > 1){
            throw new Exception("更新状态数据字典异常！");
        }
        return count;
    }

    @Override
    @Transactional
    public void insertStatus(Map dto) throws Exception {

        dictionaryMapper.insertStatus(dto);
    }

    @Override
    public int deleteStatusById(Integer status_id) throws Exception {

        int count = dictionaryMapper.deleteStatusById(status_id);
        if(count > 1){
            throw new Exception("删除状态数据字典异常！");
        }
        return count;
    }
}
